package LinkedList;

public class SingleLinkedList {
    static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) {
            this.val = val;
        }

    }
    int size;
    ListNode head;
        //头插法
    public void addFirst(int data){
        if(head == null){
            head = new ListNode(data);
            size ++;
            return;
        }
        ListNode newNode = new ListNode(data);
        newNode.next = head;
        head = newNode;
        size++;
    }
        //尾插法
    public void addLast(int data){
        if(head == null){
            head = new ListNode(data);
            size++;
            return;
        }
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = new ListNode(data);
        size++;
    }
        //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if(index < 0 || index > size){
            return false;
        }
        if(index == 0){
            addFirst(data);
        }else if(index == size){
            addLast(data);
        }else {
            ListNode cur = head;
            for(int i = 0 ; i < index-1 ; i++){
                cur = cur.next;
            }
            ListNode nextNode = cur.next;
            cur.next = new ListNode(data);
            cur.next.next = nextNode;
            size++;
        }
        return true;
    }
        //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
        //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        if(cur.val == key){
            head = cur.next;
            size--;
            return;
        }
        while(cur.next != null){
            if(cur.next.val == key){
                cur.next = cur.next.next;
                size--;
                return;
            }
        }

    }
        //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode cur = head;
        ListNode prev = head;
        while (cur.val == key){
            head = cur.next;
            cur = head;
            prev = cur;
            size--;
        }
        cur = cur.next;
        while(cur!= null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
                size--;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
    }
        //得到单链表的长度
    public int size(){
        return size;
    }
    public void display(){
        if(head == null){
            return;
        }
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    public void clear(){
        ListNode cur = head;
        while(cur != null){
            ListNode next = cur.next;
            cur.next = null;
            cur = next;
        }
        head = null;
        size = 0;
    }

}
